﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OtherDevice;
using UIDATALayer;
namespace UserData
{
    public partial class OvenState : UserControl,Interface_UserControl
    {
        public enum HeadState
        {
            烤箱内无料盘,
            待料,
            加热,
            加热完成,
            超时加热,
            烤箱有料请取出,
        };
        int n_BoxIndex = 1;
        [Category("炉子编号")]
        public int 炉子编号
        {
            set
            {
                n_BoxIndex = value;
                Panel_oven.Text = $"烤箱{n_BoxIndex}";
            }
            get { return n_BoxIndex; }
        }
        DateTime date_Begin = DateTime.Now;
        private UIDATALayer.UIDataAction<BoxStatueData> acSetOvenStatue = null;
        private UIDATALayer.UIDataAction<BoxTimeReflushData> acReflushData = null;
        private UIDATALayer.UIDataAction<OvenStatueData> acOvenStatueData = null;

        public OvenState()
        {
            InitializeComponent();
            
        }
        public void Init()
        {
            Oven_Six.EventReflushOver += ReflushOvenStatue;
            OvenMgr.GetInstance().EventSetOverStartTime += OvenStatueMgr_EventSetOverStartTime;
            OvenMgr.GetInstance().EventSetOvenState += OvenState_EventSetOvenState;

            timer1.Start();

        }
        private void OvenState_EventSetOvenState(int nBox, BoxStatus status)
        {
            if(acSetOvenStatue==null)
            {
                UIDATALayerHelper.RegistAction<BoxStatueData>(nameof(BoxStatueData), FnDoSetOvenState).Start();
                acSetOvenStatue = UIDATALayer.UIDATALayerHelper.GetRegistAction<BoxStatueData>(nameof(BoxStatueData));
                acSetOvenStatue.SetQueueNum(50);
            }
            if (nBox == 炉子编号)
            {
                BoxStatueData data = new BoxStatueData();
                data.nBox = nBox;
                data.boxStatus = status;
                acSetOvenStatue.SetData(data);
            }
        }
        private void OvenStatueMgr_EventSetOverStartTime(int nBox, DateTime date)
        {
            if(acReflushData==null)
            {
                UIDATALayerHelper.RegistAction<BoxTimeReflushData>(nameof(BoxTimeReflushData), FnDoTimeReflushData).Start();
                acReflushData = UIDATALayer.UIDATALayerHelper.GetRegistAction<BoxTimeReflushData>(nameof(BoxTimeReflushData));
            }
            if (nBox == 炉子编号)
            {
                BoxTimeReflushData data = new BoxTimeReflushData();
                data.nBox = nBox;
                data.time = date;
                acReflushData.SetData(data);
            }
        }
        public void ReflushOvenStatue(int n_Box, bool b_IsConnect, bool b_IsOpen, double d_ActTemp, double d_SetTemp, string str_Mode)
        {
            if(acOvenStatueData==null)
            {
                UIDATALayerHelper.RegistAction<OvenStatueData>(nameof(OvenStatueData), FnDoOvenStatueData).Start();
                acOvenStatueData = UIDATALayer.UIDATALayerHelper.GetRegistAction<OvenStatueData>(nameof(OvenStatueData));
                acOvenStatueData.SetQueueNum(5);
            }
            if (n_Box == 炉子编号)
            {
                OvenStatueData data = new OvenStatueData();
                data.nBox = n_Box;
                data.b_IsOpen = b_IsOpen;
                data.b_IsConnect = b_IsConnect;
                data.d_ActTemp = d_ActTemp;
                data.d_SetTemp = d_SetTemp;
                data.str_Mode = str_Mode;
                acOvenStatueData.SetData(data);
            }
        }

        private void FnDoSetOvenState(BoxStatueData data)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action(() => FnDoSetOvenState(data)));
            }
            else
            {
                if (data.nBox == 炉子编号)
                {
                    lbl_OvenState.Text = data.boxStatus.ToString();
                    if (data.boxStatus == BoxStatus.固化中)
                        lbl_OvenState.BackColor = Color.Green;
                    else if (data.boxStatus == BoxStatus.预热过程)
                        lbl_OvenState.BackColor = Color.Yellow;
                    else
                        lbl_OvenState.BackColor = Color.White;
                }
            }
        }
        private void FnDoTimeReflushData(BoxTimeReflushData data)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action(() => FnDoTimeReflushData(data)));
            }
            else
            {
                if (data.nBox == 炉子编号)
                {
                    date_Begin = data.time;
                    lbl_BeginTime.Text = data.time.ToString("HH:mm:ss");
                }
            }
        }
        private void FnDoOvenStatueData(OvenStatueData data)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action(() => FnDoOvenStatueData(data)));
            }
            else
            {
                if (data.nBox == 炉子编号)
                {
                    if (data.b_IsOpen)
                    {
                        btn_OvenStatus.Text = "烤箱运行";
                        btn_OvenStatus.Style = UIStyle.Green;
                    }
                    else
                    {
                        btn_OvenStatus.Text = "烤箱关闭";
                        btn_OvenStatus.Style = UIStyle.Red;
                    }
                    lbl_RunStatus.Text = data.str_Mode;
                    lbl_SetTem.Text = data.d_SetTemp.ToString();
                    lbl_ActTem.Text = data.d_ActTemp.ToString();
                    if (data.d_SetTemp - data.d_ActTemp > 2)
                    {
                        lbl_ActTem.BackColor = Color.FromKnownColor(System.Drawing.KnownColor.GreenYellow);
                    }
                    else
                    {
                        lbl_ActTem.BackColor = Color.FromKnownColor(System.Drawing.KnownColor.Crimson);
                    }
                    light_IsOpen.State = data.b_IsConnect ? Sunny.UI.UILightState.On : UILightState.Off;
                }
            }
        }



        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (lbl_OvenState.Text == BoxStatus.固化中.ToString())
                {
                    double d_Time= Math.Round( (DateTime.Now - date_Begin).TotalSeconds,1);
                    int n_min = (int)(d_Time / 60);
                    int n_second = (int)(d_Time % 60);
                    lbl_OvenTime.Text = $"{n_min}分{n_second}秒";
                }
                else
                {
                    lbl_OvenTime.Text = "None";
                }
            }
            catch (Exception)
            {
                
            }
        }
    }
}
